package com.rongshu.view.home.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.contract.UserInfoView;
import com.rongshu.view.home.entity.ShareInformation;
import com.rongshu.view.home.entity.UserInfo;
import com.rongshu.view.userInfo.entity.AccountInformation;
import com.rongshu.view.userInfo.entity.MessageInfo;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

public class UserInfoPresenter extends BasePresenter<UserInfoView> {

    public UserInfoPresenter(Context context) {
        super(context);
    }

    /**
     * 获取用户基本信息
     */
    public void getUserInfo(int user_id, String sign) {
        RetorfitService.getUserInfo(user_id, sign)
                .subscribe(new Subscriber<UserInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserInfo userInfo) {
                        if (getView() != null) {
                            getView().onUserInfo(userInfo);
                        }
                    }
                });
    }

    /**
     * PC登录
     *
     * @param uip
     * @param userId
     */
    public void loginPC(String uip, int userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        map.put("uip", uip);
        RetorfitService.sweepQrCodeLogin(userId, uip,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                ToastUtils.show(object.getErrMsg());
            }
        });
    }

    /**
     * 分享信息
     *
     * @param userId
     * @param sign
     */
    public void getShareUrl(int userId, String sign) {
        RetorfitService.getShareUrl(userId,
                sign)
                .subscribe(new Subscriber<ShareInformation>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ShareInformation object) {
                        if (getView() != null) {
                            getView().onShareApp(object);
                        }
                    }
                });
    }

    /**
     * 获取账号信息(消息、关注、粉丝、获赞、笔友会、卡包、榕树叶数量和用户等创作、阅读级等)
     */
    public void getAccountModel(int user_id, String sign) {
        RetorfitService.getAccountModel(user_id, sign)
                .subscribe(new Subscriber<AccountInformation>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(AccountInformation accountInformation) {
                        if (getView() != null) {
                            getView().onAccountInfo(accountInformation);
                        }
                    }
                });
    }

    /**
     * 获取我的消息页面的未读信息数
     */
    public void getMsgCount(int user_id, String sign) {
        RetorfitService.getMsgCount(user_id, sign)
                .subscribe(new Subscriber<MessageInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(MessageInfo info) {
                        if (getView() != null) {
                            getView().onMsgCount(info);
                        }
                    }
                });
    }
}
